package com.itic.hpp.wx;

import com.itic.appbase.framework.utils.WXPay.WeixinUtil;
import com.itic.system.order.order.service.OrderService;
import com.itic.system.pay.userPay.service.PayUserLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

@Controller
@RequestMapping(value = "/hpp/wx/pay")
public class WXPayInterface  {
    @Autowired
    private PayUserLogService payUserLogService;


    @RequestMapping(value = "/success")
    public String paySuccess(HttpServletRequest request , HttpServletResponse response){
        //用于处理结束后返回的xml
        StringBuffer sb = new StringBuffer();
        InputStream in = null;
        ByteArrayOutputStream out = null;
        try {
            in = request.getInputStream();
            out = new ByteArrayOutputStream();
            int len = 0;
            byte[] b = new byte[1024];
            while((len = in.read(b)) != -1){
                out.write(b, 0, len);
            }
            //将流 转为字符串
            String result = new String(out.toByteArray(), "utf-8");
            Map<String, String> map = WeixinUtil.getNotifyUrl(result);
            String return_code = map.get("return_code").toString().toUpperCase();
           /* String attach = map.get("attach").toString().toUpperCase();
            String key = "&key="+attach;*/
            if(return_code.equals("SUCCESS")){
                //进行签名验证，看是否是从微信发送过来的，防止资金被盗
                //if(WeixinUtil.verifyWeixinNotify(map, key)){
                //签名验证成功后按照微信要求返回的xml
                sb.append("<xml>");
                sb.append("<return_code><![CDATA[SUCCESS]]></return_code>");
                sb.append("<return_msg><![CDATA[OK]]></return_msg>");
                sb.append("</xml>");
                //支付完成时间
                String finishTime = map.get("time_end");
                //订单号
                String out_trade_no = map.get("out_trade_no");
                //实付款
                String total_fee = map.get("total_fee");
                //信支付订单号
                String transaction_id = map.get("transaction_id");
                //用户openId
                String openid = map.get("openid");
                //商家appId
                String appid = map.get("appid");
                //商户号
                String mch_id = map.get("mch_id");
                //处理自己的订单业务
                this.payUserLogService.paySuccessDealOrder(appid, out_trade_no, finishTime, total_fee, transaction_id, openid, mch_id);
                return sb.toString();
                //}
            }else{
                sb.append("<xml>");
                sb.append("<return_code><![CDATA[FAIL]]></return_code>");
                sb.append("<return_msg><![CDATA[sign check error]]></return_msg>");
                sb.append("</xml>");
                return sb.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                out.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        sb.append("<xml>");
        sb.append("<return_code><![CDATA[FAIL]]></return_code>");
        sb.append("<return_msg><![CDATA[xml error]]></return_msg>");
        sb.append("</xml>");
        return sb.toString();

    }
}
